using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisSection : AnalysisStatement
{
    internal AnalysisSection(
        SectionStatement syntaxNode,
        AnalysisIdentifier? nameIdentifier,
        AnalysisNode? parent) :
        base(AnalysisBoundKinds.SectionStatement, syntaxNode, parent)
    {
        if (nameIdentifier != null)
        {
            NameIdentifier = nameIdentifier;
            NameIdentifier.Parent = this;
        }

        Body = AnalysisNodeFactory.Block(syntaxNode.Body, this, null);
    }


    public AnalysisIdentifier? NameIdentifier { get; }


    public AnalysisBlock Body { get; }

    public override int ChildrenCount => 1 + (NameIdentifier == null ? 0 : 1);

    public override IEnumerable<AnalysisNode> GetChildren()
    {
        if (NameIdentifier == null)
        {
            return [Body];
        }

        return [NameIdentifier, Body];
    }


    internal override void AddChild(AnalysisNode child)
    {
        throw new NotSupportedException("AnalysisSection is read-only.");
    }
}


public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个分区节点
    /// </summary>
    /// <param name="syntaxNode">原始语法节点</param>
    /// <param name="nameIdentifier">命名节点，可以忽略</param>
    /// <param name="parent">父节点，可以忽略</param>
    public static AnalysisSection Section(
        SectionStatement syntaxNode,
        AnalysisIdentifier? nameIdentifier,
        AnalysisNode? parent)
    {
        return new AnalysisSection(syntaxNode, nameIdentifier, parent);
    }
}